﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Common.Entity;
using ESOL.Business.Prod;
using ESOL.Common;

namespace ESOL.WinUI.Manufacture
{
    public partial class frmMaintainTechnicRatio : BaseForm.frmBaseDialogyMaintainForm
    {
        #region vars
        public ProdTechnicEntity prodTechnicEntity { set; get; }
        private ProdTechnicRatioEntity prodTechnicRatioEntity { set; get; }
        private List<ProdTechnicRatioEntity> delProdTechnicRatioEntities = new List<ProdTechnicRatioEntity>();
        private DataRow drSelected;
        #endregion

        #region ctors
        public frmMaintainTechnicRatio()
        {
            InitializeComponent();
        }
        #endregion

        #region functions
        private void BindGridData()
        {
            if (prodTechnicEntity == null)
            {
                return;
            }
            DataTable dt = ProdTechnicRatioBs.GetDtAllByTechnicID(prodTechnicEntity.TechnicId);
            dt.AcceptChanges();
            gvMain.DataSource = dt;
        }

        private void NewLine()
        {
            DataTable dt = gvMain.DataSource as DataTable;
            DataRow dr = dt.NewRow();
            dr["TECHNIC_ID"] = prodTechnicEntity.TechnicId;
            dr["VALIDITY_FLAG"] = 0;
            dt.Rows.Add(dr);
        }

        private void DelLine()
        {
            if (prodTechnicRatioEntity == null || drSelected==null)
            {
                return;
            }
            DataTable dt = gvMain.DataSource as DataTable;
            dt.Rows.Remove(drSelected);
            delProdTechnicRatioEntities.Add(prodTechnicRatioEntity);
        }

        private void Valid()
        {
            if (drSelected == null)
            {
                return;
            }
            drSelected["VALIDITY_FLAG"] = (int)ValidityFlag.有效;
        }

        private void Invalid()
        {
            if (drSelected == null)
            {
                return;
            }
            drSelected["VALIDITY_FLAG"] = (int)ValidityFlag.无效;
        }

        private bool CheckData()
        {
            DataTable dt = gvMain.DataSource as DataTable;
            bool flag=false;
            foreach(DataRow dr in dt.Rows)
            {
                if ((int)dr["VALIDITY_FLAG"] == (int)ValidityFlag.有效)
                {
                    flag = true;
                }
                if (string.IsNullOrEmpty(dr["RATIO"].ToString()))
                {
                    MessageBox.Show("批量系数不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
                if (string.IsNullOrEmpty(dr["QTY_FROM"].ToString()))
                {
                    MessageBox.Show("最小数量不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
                if (string.IsNullOrEmpty(dr["QTY_TO"].ToString()))
                {
                    MessageBox.Show("最大数量不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
            }
            if (flag == false)
            {
                MessageBox.Show("请选择一条活动数据！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }

        private bool Save()
        {
            DataTable dt=gvMain.DataSource as DataTable;
            if (ProdTechnicRatioBs.Save(dt, delProdTechnicRatioEntities))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region events
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindGridData();
        }

        private void tsmNewLine_Click(object sender, EventArgs e)
        {
            NewLine();
        }

        private void tsmDelLine_Click(object sender, EventArgs e)
        {
            DelLine();
        }

        private void repositoryItemCheckEdit1_Click(object sender, EventArgs e)
        {
            if (drSelected == null)
            {
                return;
            }
            if ((int)drSelected["VALIDITY_FLAG"] == (int)ValidityFlag.无效)
            {
                Valid();
            }
            else if ((int)drSelected["VALIDITY_FLAG"] == (int)ValidityFlag.有效)
            {
                Invalid();
            }
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            drSelected = gridView1.GetDataRow(e.FocusedRowHandle);
            prodTechnicRatioEntity = new ProdTechnicRatioEntity(drSelected);
        }

        protected override void Click_Save()
        {
            if (CheckData()==false)
            {
                return;
            }
            if (Save())
            {
                MessageBox.Show("保存成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("保存失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
